#include "stdafx.h"
#include <time.h>
#include <sys/utime.h>
#include <iostream>
#include "logging.h"
#include <Windows.h>
#include <string.h>
#include <Shlwapi.h>
#include <io.h>

void SetLoggingLevel(int Level)
{
	g_LoggingLevel = (TLogginLevel)Level;
}

void SetLogPath(wchar_t *Path)
{
	g_LoggingPath = Path;
}

void SetLogFileName(wchar_t *Filename)
{
	g_LoggingFileName = Filename;
}

char *GetMessageFromWindowsMessage(UINT Message) {
	if (Message == WM_NULL) return "WM_NULL";
	else if (Message == WM_CREATE) return "WM_CREATE";
	else if (Message == WM_DESTROY) return "WM_DESTROY";
	else if (Message == WM_MOVE) return "WM_MOVE";
	else if (Message == WM_SIZE) return "WM_SIZE";
	else if (Message == WM_ACTIVATE) return "WM_ACTIVATE";
	else if (Message == WM_SETFOCUS) return "WM_SETFOCUS";
	else if (Message == WM_KILLFOCUS) return "WM_KILLFOCUS";
	else if (Message == WM_ENABLE) return "WM_ENABLE";
	else if (Message == WM_SETREDRAW) return "WM_SETREDRAW";
	else if (Message == WM_SETTEXT) return "WM_SETTEXT";
	else if (Message == WM_GETTEXT) return "WM_GETTEXT";
	else if (Message == WM_PAINT) return "WM_PAINT";
	else if (Message == WM_CLOSE) return "WM_CLOSE";
	else if (Message == WM_QUIT) return "WM_QUIT";
	else if (Message == WM_ERASEBKGND) return "WM_ERASEBKGND";
	else if (Message == WM_SHOWWINDOW) return "WM_SHOWWINDOW";
	else if (Message == WM_CANCELMODE) return "WM_CANCELMODE";
	else if (Message == WM_SETCURSOR) return "WM_SETCURSOR";
	else if (Message == WM_PAINTICON) return "WM_PAINTICON";
	else if (Message == WM_ICONERASEBKGND) return "WM_ICONERASEBKGND";
	else if (Message == WM_DRAWITEM) return "WM_DRAWITEM";
	else if (Message == WM_MEASUREITEM) return "WM_MEASUREITEM";
	else if (Message == WM_DELETEITEM) return "WM_DELETEITEM";
	else if (Message == WM_SETFONT) return "WM_SETFONT";
	else if (Message == WM_GETFONT) return "WM_GETFONT";
	else if (Message == WM_SETHOTKEY) return "WM_SETHOTKEY";
	else if (Message == WM_GETHOTKEY) return "WM_GETHOTKEY";
	else if (Message == WM_WINDOWPOSCHANGING) return "WM_WINDOWPOSCHANGING";
	else if (Message == WM_WINDOWPOSCHANGED) return "WM_WINDOWPOSCHANGED";
	else if (Message == WM_POWER) return "WM_POWER";
	else if (Message == WM_COPYDATA) return "WM_COPYDATA";
	else if (Message == WM_USERCHANGED) return "WM_USERCHANGED";
	else if (Message == WM_CONTEXTMENU) return "WM_CONTEXTMENU";
	else if (Message == WM_STYLECHANGING) return "WM_STYLECHANGING";
	else if (Message == WM_STYLECHANGED) return "WM_STYLECHANGED";
	else if (Message == WM_DISPLAYCHANGE) return "WM_DISPLAYCHANGE";
	else if (Message == WM_GETICON) return "WM_GETICON";
	else if (Message == WM_SETICON) return "WM_SETICON";
	else if (Message == WM_KEYFIRST) return "WM_KEYFIRST";
	else if (Message == WM_KEYUP) return "WM_KEYUP";
	else if (Message == WM_CHAR) return "WM_CHAR";
	else if (Message == WM_DEADCHAR) return "WM_DEADCHAR";
	else if (Message == WM_SYSKEYDOWN) return "WM_SYSKEYDOWN";
	else if (Message == WM_SYSKEYUP) return "WM_SYSKEYUP";
	else if (Message == WM_COMMAND) return "WM_COMMAND";
	else if (Message == WM_SYSCOMMAND) return "WM_SYSCOMMAND";
	else if (Message == WM_TIMER) return "WM_TIMER";
	else if (Message == WM_HSCROLL) return "WM_HSCROLL";
	else if (Message == WM_VSCROLL) return "WM_VSCROLL";
	else if (Message == WM_MENUSELECT) return "WM_MENUSELECT";
	else if (Message == WM_MOUSEFIRST) return "WM_MOUSEFIRST";
	else if (Message == WM_LBUTTONDOWN) return "WM_LBUTTONDOWN";
	else if (Message == WM_LBUTTONUP) return "WM_LBUTTONUP";
	else if (Message == WM_LBUTTONDBLCLK) return "WM_LBUTTONDBLCLK";
	else if (Message == WM_RBUTTONDOWN) return "WM_RBUTTONDOWN";
	else if (Message == WM_RBUTTONUP) return "WM_RBUTTONUP";
	else if (Message == WM_RBUTTONDBLCLK) return "WM_RBUTTONDBLCLK";
	else if (Message == WM_SIZING) return "WM_SIZING";
	else if (Message == WM_MOVING) return "WM_MOVING";
	else if (Message == WM_MOUSEHOVER) return "WM_MOUSEHOVER";
	else if (Message == WM_MOUSELEAVE) return "WM_MOUSELEAVE";
	else if (Message == WM_HOTKEY) return "WM_HOTKEY";
	else if (Message == WM_PRINT) return "WM_PRINT";
	else if (Message == WM_APPCOMMAND) return "WM_APPCOMMAND";
	else if (Message == WM_THEMECHANGED) return "WM_THEMECHANGED";
	else if (Message == WM_APP) return "WM_APP";
	else if (Message == WM_INPUT) return "WM_INPUT";
	else if (Message == WM_USER) return "WM_USER";
	else if (Message == WM_SYSCOMMAND) {
		return "WM_SYSCOMMAND";
	}
	else return "";

}

char *GetWParamFromWindowsMessage(UINT Message, WPARAM wparam) {
	if (Message == WM_SYSCOMMAND){
		if ((wparam & 0xFFF0) == SC_TASKLIST)
			return "SC_TASKLIST";
		else if ((wparam & 0xFFF0) == SC_CLOSE)
			return "SC_CLOSE";
		else if ((wparam & 0xFFF0) == SC_CONTEXTHELP)
			return "SC_CONTEXTHELP";
		else if ((wparam & 0xFFF0) == SC_DEFAULT)
			return "SC_DEFAULT";
		else if ((wparam & 0xFFF0) == SC_HOTKEY)
			return "SC_HOTKEY";
		else if ((wparam & 0xFFF0) == SC_HSCROLL)
			return "SC_HSCROLL";
		else if ((wparam & 0xFFF0) == SCF_ISSECURE)
			return "SCF_ISSECURE";
		else if ((wparam & 0xFFF0) == SC_KEYMENU)
			return "SC_KEYMENU";
		else if ((wparam & 0xFFF0) == SC_MAXIMIZE)
			return "SC_MAXIMIZE";
		else if ((wparam & 0xFFF0) == SC_MINIMIZE)
			return "SC_MINIMIZE";
		else if ((wparam & 0xFFF0) == SC_MONITORPOWER)
			return "SC_MONITORPOWER";
		else if ((wparam & 0xFFF0) == SC_MOUSEMENU)
			return "SC_MOUSEMENU";
		else if ((wparam & 0xFFF0) == SC_MOVE)
			return "SC_MOVE";
		else if ((wparam & 0xFFF0) == SC_NEXTWINDOW)
			return "SC_NEXTWINDOW";
		else if ((wparam & 0xFFF0) == SC_PREVWINDOW)
			return "SC_PREVWINDOW";
		else if ((wparam & 0xFFF0) == SC_RESTORE)
			return "SC_RESTORE";
		else if ((wparam & 0xFFF0) == SC_SCREENSAVE)
			return "SC_SCREENSAVE";
		else if ((wparam & 0xFFF0) == SC_SIZE)
			return "SC_SIZE";
		else if ((wparam & 0xFFF0) == SC_VSCROLL)
			return "SC_VSCROLL";
		else {
			//char buf[30] = "";
			//sprintf_s(buf, "%p", wparam);
			return "";
		}

	}
	else {
		//char buf[30] = "";
		//sprintf_s(buf, "%p", wparam);
		return "";
	}
}

char *GetMessageDatePart()
{
	char DatePart[35];
	char fromTime[35];
	time_t now = time(0);
	struct tm gmt;
	localtime_s(&gmt, &now);

	strftime(fromTime, 35, "%Y%m%d %I:%M:%S", &gmt);
	return fromTime;
}

char *GetMessageTextPart(char *Msg)
{
	char MessagePart[950];
	strcpy_s(MessagePart, 950, "<TEXT>");
	strcat_s(MessagePart, 950, Msg);
	strcat_s(MessagePart, 950, "</TEXT>");

	return MessagePart;
}

char *GetLoggingLevelString(TLogginLevel LoggingLevel) {
	char *MessageTyp;

	switch (LoggingLevel){
	case LOG_ERROR:
		MessageTyp = "Error";
		break;
	case LOG_HINT:
		MessageTyp = "Hint";
		break;
	case LOG_ACTIONS:
		MessageTyp = "Action";
		break;
	case LOG_ACTION_MOUSE:
		MessageTyp = "Action Mouse";
		break;
	case LOG_ACTION_KEYBOARD:
		MessageTyp = "Action Keyboard";
		break;
	case LOG_DEBUG:
		MessageTyp = "Debug";
		break;
	case LOG_MESSAGES:
		MessageTyp = "Messages";
		break;
	Default:
		MessageTyp = "Unknown";
		break;
	}

	return MessageTyp;
}

bool LogMessage(char *Msg, TLogginLevel LoggingLevel)
{
	//											 WinKStart\\Log\\ 

	wchar_t fname[_MAX_PATH] = L"d:\\testlogging.xml"; // L"%LOCALAPPDATA%\\";// WinKStartLog.txt";
	if (sizeof(g_LoggingPath) == 0 || sizeof(g_LoggingFileName) == 0)
		return false; //can't logging messages

	StrCpyW(fname, g_LoggingPath);

	if (!PathFileExistsW(fname))
		CreateDirectoryW(fname, NULL);

	wcscat_s(fname, _MAX_PATH, g_LoggingFileName);

	//DoEnvironmentSubst(fname, _countof(fname));
	char buf[1030];
	char MsgPart[35];

	//strcpy_s(buf, 900, "kfjaslfj");

	strcpy_s(buf, 1030, "<MESSAGE><DATE>");
	strcpy_s(MsgPart, 35, GetMessageDatePart());
	strcat_s(buf, 1030, MsgPart);
	strcat_s(buf, 1030, "</DATE>");
	strcat_s(buf, 1030, "<TYPE>");
	strcat_s(buf, 1030, GetLoggingLevelString(LoggingLevel));
	strcat_s(buf, 1030, "</TYPE>");
	strcat_s(buf, 1030, GetMessageTextPart(Msg));

	strcat_s(buf, 1030, "</MESSAGE>");
	//TODO: add logginglevel to logstring
	//strcat_s(buf, 900, Msg);

	if (g_LoggingLevel & LoggingLevel)
	{
		FILE *f;
		if (_wfopen_s(&f, fname, L"a") == 0)
		{
			strcat_s(buf, 1030, "\n");
			fprintf(f, buf); //"Failed to hook Explorer - error=0x%X\r\n"

			fclose(f);
		}
	}
	return true;
}


bool LogMessage(MSG Message, char *Source, TLogginLevel LoggingLevel)
{
	wchar_t fname[_MAX_PATH] = L"d:\\testlogging.xml"; // L"%LOCALAPPDATA%\\";// WinKStartLog.txt";
	if (sizeof(g_LoggingPath) == 0 || sizeof(g_LoggingFileName) == 0)
		return false; //can't logging messages

	StrCpyW(fname, g_LoggingPath);

	if (!PathFileExistsW(fname))
		CreateDirectoryW(fname, NULL);

	wcscat_s(fname, _MAX_PATH, g_LoggingFileName);

	//DoEnvironmentSubst(fname, _countof(fname));
	char buf[1030];
	char MsgPart[35];
	if (g_LoggingLevel & LoggingLevel)
	{	

		char buf[1030];
		char MsgPart[35];

		char MsgName[50];
		char MsgWParam[30];

		//strcpy_s(buf, 900, "kfjaslfj");
		strcpy_s(MsgName, 50, GetMessageFromWindowsMessage(Message.message));
		if (MsgName != "\0"){
			strcpy_s(buf, 1030, "<MESSAGE><DATE>");
			strcpy_s(MsgPart, 35, GetMessageDatePart());
			strcat_s(buf, 1030, MsgPart);
			strcat_s(buf, 1030, "</DATE>");
			strcat_s(buf, 1030, "<SOURCE>");
			strcat_s(buf, 1030, Source);
			strcat_s(buf, 1030, "</SOURCE>");
			strcat_s(buf, 1030, "<MSG>");
			strcat_s(buf, 1030, MsgName);
			strcat_s(buf, 1030, "</MSG>");
			strcat_s(buf, 1030, "<WPARAM>");
			strcpy_s(MsgWParam, 30, GetWParamFromWindowsMessage(Message.message, Message.wParam));
			strcat_s(buf, 1030, MsgWParam);
			strcat_s(buf, 1030, "</WPARAM>");
			strcat_s(buf, 1030, "<MSGVALUE>");
			char valbuf[30] = "";
			sprintf_s(valbuf, "%d", Message.message);
			strcat_s(buf, 1030, valbuf);
			strcat_s(buf, 1030, "</MSGVALUE>");
			strcat_s(buf, 1030, "<HWND>");
			char valbuf2[30] = "";
			sprintf_s(valbuf2, "%p", Message.hwnd);
			strcat_s(buf, 1030, valbuf2);
			strcat_s(buf, 1030, "<HWND>");
			strcat_s(buf, 1030, "</MESSAGE>");

			//if (g_LoggingLevel & LoggingLevel)
			{
				FILE *f;
				if (_wfopen_s(&f, fname, L"a") == 0)
				{
					strcat_s(buf, 1030, "\n");
					fprintf(f, buf); //"Failed to hook Explorer - error=0x%X\r\n"

					fclose(f);
				}
			}
		}
	}
	return true;
}

bool LogMessage(wchar_t *Msg, wchar_t *Source, HWND Handle){
	wchar_t fname[_MAX_PATH] = L"d:\\testlogging2.xml";
	wchar_t buf[1030];

	StrCpyW(buf, L"<MESSAGE><DATE>");
	StrCpyW(buf, L"</DATE>");
	StrCpyW(buf, L"<SOURCE>");
	StrCpyW(buf, Source);
	StrCpyW(buf, L"</SOURCE>");
	StrCpyW(buf, L"<TEXT>");
	StrCpyW(buf, Msg);
	StrCpyW(buf, L"</TEXT>");
	StrCpyW(buf, L"<HWND>");
	swprintf_s(buf, L"%p", Handle);
	StrCpyW(buf, L"<HWND>");
	StrCpyW(buf, L"</MESSAGE>");


	FILE *f;
	if (_wfopen_s(&f, fname, L"a") == 0)
	{
		StrCpyW(buf, L"\n");
		fwprintf(f, buf);
		fclose(f);
	}

	return true;
}

bool LogMessage(char *Msg, char *Value, TLogginLevel LoggingLevel){
	char DebugMsg[250] = "";
	strcat_s(DebugMsg, 250, Msg);
	strcat_s(DebugMsg, 250, Value);
	LogMessage(DebugMsg, LoggingLevel);

	return true;
}

bool LogMessage(char *Msg, int Value, TLogginLevel LoggingLevel)
{
	char DebugMsg[250] = "";
	strcat_s(DebugMsg, 250, Msg);
	char buf[30] = "";
	sprintf_s(buf, "%d", Value);
	strcat_s(DebugMsg, 250, buf);

	LogMessage(DebugMsg, LoggingLevel);

	return true;
}

bool LogMessage(char *Msg, HWND Handle, TLogginLevel LoggingLevel)
{
	char DebugMsg[250] = "";
	strcat_s(DebugMsg, 250, Msg);
	char buf[30] = "";
	sprintf_s(buf, "%p", Handle);
	strcat_s(DebugMsg, 250, buf);

	LogMessage(DebugMsg, LoggingLevel);

	return true;
}

bool LogMessage(char *Msg, RECT Range, TLogginLevel LogType)
{
	char DebugMsg[270] = "";
	strcat_s(DebugMsg, 270, Msg);
	char buf[70] = "";
	sprintf_s(buf, "Left: %d, Top: %d, Right: %d, Bottom: %d", Range.left, Range.top, Range.right, Range.bottom);
	strcat_s(DebugMsg, 270, buf);

	LogMessage(DebugMsg, LogType);

	return true;
}